package com.yilin.platform.controller;

import com.google.gson.reflect.TypeToken;
import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.utils.MapUtil;
import com.yilin.platform.base_core.interceptor.EncryptInterceptor;
import com.yilin.platform.base_core.tools.bank.BankCard;
import com.yilin.platform.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/platform/bank/")
public class CommBankController extends BaseController {

    @Resource
    IUserService userService;

    /**
     * 获取银行卡列表
     */
    @PostMapping(value = "getBankCardList")
    @EncryptInterceptor
    public ReturnData<List<BankCard>> getBankCardList(String bankShortName) {
        Map<String, Object> objectMap = MapUtil.generate(map -> map.put("bankShortName", bankShortName));
        TypeToken<List<BankCard>> typeToken = new TypeToken<List<BankCard>>() {};
        String apiToken = getLoginUser().getToken();
        List<BankCard> bankcardList = userService.request("/capital/assets/getBankCardList",apiToken, objectMap, typeToken);
        return jsonView(SUCCESS_CODE, "获取成功", bankcardList);
    }

    /**
     * 获取城市列表
     */
    @PostMapping(value = "getCityList")
    @EncryptInterceptor
    public ReturnData<List<Object>> getCityList(String cityName) {
        Map<String, Object> objectMap = MapUtil.generate(map -> map.put("cityName", cityName));
        TypeToken<List<Object>> typeToken = new TypeToken<List<Object>>() {};
        String apiToken = getLoginUser().getToken();
        List<Object> bankcardList = userService.request("/system/param/getCityList",apiToken, objectMap, typeToken);
        return jsonView(SUCCESS_CODE, "获取成功", bankcardList);
    }

    /**
     * 获取银行列表
     */
    @PostMapping(value = "getBankList")
    @EncryptInterceptor
    public ReturnData<Object> getBankList(String bankName) {
        Map<String, Object> objectMap = MapUtil.generate(map -> map.put("bankName", bankName));
        TypeToken<List<Object>> typeToken = new TypeToken<List<Object>>() {};
        String apiToken = getLoginUser().getToken();
        Object bankList = userService.request("/system/param/getBankList",apiToken, objectMap, typeToken);
        return jsonView(SUCCESS_CODE, "获取成功", bankList);
    }

    /**
     * 获取开户行列表
     */
    @PostMapping(value = "queryBankCodePage")
    @EncryptInterceptor
    public ReturnData<Object> queryBankCodePage(String bankShortName, String cityName, String bankName, Integer page, Integer limit) {
        TypeToken<Object> typeToken = new TypeToken<Object>() {};
        String apiToken = getLoginUser().getToken();
        Object bankcardList = userService.request("/system/param/queryBankCodePage",apiToken, MapUtil.generate(map -> {
            map.put("bankShortName", bankShortName);
            map.put("cityName", cityName);
            map.put("bankName", bankName);
            map.put("page", page);
            map.put("limit", limit);
        }), typeToken);
        return jsonView(SUCCESS_CODE, "获取成功", bankcardList);
    }
}
